package com.airsystem.pos.bengkel.controller.dialog;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import com.airsystem.pos.bengkel.component.TableFrame;
import com.airsystem.pos.bengkel.entity.PesanSparepart;
import com.airsystem.pos.bengkel.entity.wrapper.PesanSparepartWrapper;
import com.airsystem.pos.bengkel.interfaces.ILookup;
import com.airsystem.pos.bengkel.interfaces.IParser;
import com.airsystem.pos.bengkel.interfaces.IServices;
import com.airsystem.pos.bengkel.interfaces.ISystem;
import com.airsystem.pos.bengkel.model.dialog.SparepartOrderDialogModel;
import com.airsystem.pos.bengkel.service.PesanSparepartServices;
import com.airsystem.pos.bengkel.service.converter.SparepartEntityConverter;
import com.airsystem.pos.bengkel.util.Preconditions;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */
public class SparepartOrderDialogController extends SparepartOrderDialogModel implements ILookup, ISystem {
	private static final Logger LOG = Logger.getLogger(SparepartOrderDialogController.class.getSimpleName());

	private final IServices<PesanSparepart> service;

	private final IParser<PesanSparepartWrapper, PesanSparepart> converter;

	private final Map<Integer, PesanSparepartWrapper> mapHistory;

	public SparepartOrderDialogController() {
		super();

		service = new PesanSparepartServices();

		converter = new SparepartEntityConverter();

		mapHistory = new HashMap<Integer, PesanSparepartWrapper>();
	}

	public void setup() {
		TableFrame.deleteTable(tableData, tableModel);
	}

	public void get() {
		int selectedRow = tableData.getSelectedRow();
		pesanSparepartWrapper = getPesanSparepartWrapper(selectedRow);
	}

	public void reset() {
		((JTextField) searchField.getDateEditor().getUiComponent()).setText("");
		searchCombo.requestFocus();
	}

	public void category() {
		throw new UnsupportedOperationException("Not Implemented.");
	}

	public void search() {
		try {
			Preconditions.checkEmpty(searchField);
		} catch (IllegalArgumentException e) {
			JOptionPane.showMessageDialog(this, "Silahkan input data!", "Pesan", JOptionPane.WARNING_MESSAGE);
			reset();

			return;
		}

		try {
			setup();
			Iterable<PesanSparepart> orders = service.select(getSearchData());

			int index = 0;
			for (PesanSparepart item : orders) {
				PesanSparepartWrapper wrapper = converter.doParse(item);
				mapHistory.put(index++, wrapper);

				Vector<String> vector = new Vector<String>();
				vector.add(wrapper.getTanggal());
				vector.add(wrapper.getTotalProduk());
				vector.add(wrapper.getTotalHarga());
				vector.add(wrapper.getMetode());

				tableModel.addRow(vector);
			}

			tableData.setModel(tableModel);
		} catch (IllegalArgumentException e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Terdapat kesalahan pada input!", "Pesan", JOptionPane.WARNING_MESSAGE);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	public void load() {
		try {
			Iterable<PesanSparepart> orders = service.select(new PesanSparepart());

			int index = 0;
			for (PesanSparepart item : orders) {
				PesanSparepartWrapper wrapper = converter.doParse(item);
				mapHistory.put(index++, wrapper);

				Vector<String> vector = new Vector<String>();
				vector.add(wrapper.getTanggal());
				vector.add(wrapper.getTotalProduk());
				vector.add(wrapper.getTotalHarga());
				vector.add(wrapper.getMetode());

				tableModel.addRow(vector);
			}
			tableData.setModel(tableModel);

			this.setVisible(true);
		} catch (Exception e) {
			this.setVisible(false);
			LOG.error(e.getMessage(), e);
			JOptionPane.showMessageDialog(this, "Load data gagal!", "Pesan", JOptionPane.ERROR_MESSAGE);
		}
	}

	protected PesanSparepartWrapper getPesanSparepartWrapper(int key) {
		return mapHistory.get(key);
	}
}